package com.qd.hik.service;

import com.cdqidi.httpclientbootstart.util.HttpUtil;
import com.cdqidi.util.JacksonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qd.hik.base.HkBaseTokenDTO;
import com.qd.hik.dto.HkBaseHttpResp;
import com.qd.hik.dto.invoice.*;
import com.qd.hik.token.HkAccessToken;
import com.qd.hik.token.HkAccessTokenManager;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 电子发票
 *
 * @author sjk
 * @create 2024-05-29 15:36
 * @since 1.0.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
@HkAccessToken
public class HkInvoiceService {
    private final HkBaseTokenDTO hkBaseTokenDTO;
    private final HttpUtil httpUtil;
    private final ObjectMapper objectMapper;

    /**
     * 2.5.1 可开票停车账单
     */
    private static final String INVOICE_INVOICE_ABLE_PARKING_BILLS_URL="/artemis/api/v1/invoice/invoiceableParkingBills";
    /**
     * 2.5.2 可开票包月账单
     */
    private static final String INVOICE_INVOICE_ABLE_BAG_BILLS_URL="/artemis/api/v1/invoice/invoiceableBagBills";
    /**
     * 2.5.3 获取开票地址
     */
    private static final String INVOICE_GET_INVOICE_URL_URL="/artemis/api/v1/invoice/getInvoiceUrl";
    /**
     * 2.5.4 获取开票记录
     */
    private static final String INVOICE_INVOICE_RECORDS_URL="/artemis/api/v1/invoice/invoiceRecords";
    /**
     * 2.5.5 获取开票详情
     */
    private static final String INVOICE_INVOICE_DETAIL_URL="/artemis/api/v1/invoice/invoiceDetail";

    /**
     * 2.5.1  可开票停车账单
     *
     * @param req 请求参数
     * {
     * "pageNo":1,
     * "pageSize": 10,
     * "driverId":1
     * }
     */
    @SneakyThrows
    public HkBaseHttpResp<HkInvoiceAbleParkingBillsResp> invoiceAbleParkingBills(HkInvoiceAbleParkingBillsReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + INVOICE_INVOICE_ABLE_PARKING_BILLS_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.5.2 可开票包月账单
     *
     * @param req 请求参数
     */
    @SneakyThrows
    public HkBaseHttpResp<HkInvoiceAbleBagBillsResp> invoiceAbleBagBills(HkInvoiceAbleParkingBillsReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + INVOICE_INVOICE_ABLE_BAG_BILLS_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.5.3 获取开票地址
     *
     * @param req 请求参数
     */
    @SneakyThrows
    public HkBaseHttpResp<HkGetInvoiceResp> getInvoiceUrl(HkGetInvoiceReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + INVOICE_GET_INVOICE_URL_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.5.4 获取开票记录
     *
     * @param req 请求参数
     */
    @SneakyThrows
    public HkBaseHttpResp<HkInvoiceRecordsResp> invoiceRecords(HkInvoiceAbleParkingBillsReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + INVOICE_INVOICE_RECORDS_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }

    /**
     * 2.5.5 获取开票详情
     *
     * @param req 请求参数
     */
    @SneakyThrows
    public HkBaseHttpResp<HkInvoiceDetailResp> invoiceDetail(HkInvoiceDetailReq req) {
        String json = JacksonUtil.getObjectMapper().writeValueAsString(req);
        String post = httpUtil.sendHttpsPost(hkBaseTokenDTO.getBaseUrl() + INVOICE_INVOICE_DETAIL_URL, json, Map.of("access_token", HkAccessTokenManager.getToken().getAccessToken()));
        return objectMapper.readValue(post, new TypeReference<>() {
        });
    }
}